package com.mcxx.modules.system.controller;

import com.mcxx.modules.miniSalvation.check.param.CompareBizParam;
import com.mcxx.modules.miniSalvation.family.dto.MessageDTO;
import com.mcxx.modules.miniSalvation.family.entity.IdCardAllBizEntity;
import com.mcxx.modules.miniSalvation.family.service.read.BizCheckService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.*;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;


@Validated
@Api("业务核对控制类")
@RestService({"bizCheck/v2"})
public class BizCheckController extends DefaultRestService {

    private static final Logger logger = LoggerFactory.getLogger(BizCheckController.class);

    @Autowired
    private BizCheckService bizCheckService;


    @PostMapping({"downLoadCompareTemplate"})
    public void downLoadCompareTemplate(HttpServletResponse response) throws IOException {
        logger.info("下载数据核对导入模板开始");
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/bakhtml/bizCheckTemplate.xls");
        byte[] byt = new byte[resourceAsStream.available()];
        byt = IOUtils.toByteArray(resourceAsStream);
        FileExportUtil.downLoadTemplate(response, byt, getCurUserAccount().getAreaName(),"数据核对导入模板.xls");
//        ZipInfoUtil.exportZipByte(response,"数据核对导入模板.xls", byt, this.getCurUserId());
        logger.info("下载数据核对导入模板结束");
    }


    @PostMapping(value = "importCompareFile")
    @ApiOperation(value = "从excel中读取要核对的身份证号")
    public BaseResult<List<IdCardAllBizEntity>> importCompareFile(@RequestPart("file") MultipartFile file) {
        logger.info("读取要核对的身份证号");
        if (CommonUtil.invalidArgs(file)) {
            setError(FailureCode.ERR_10000.getMessage());
        }
        BaseResult<List<IdCardAllBizEntity>> result = null;
        result = bizCheckService.generateCompareData(file,getCurUserAccount());

        return result;
    }




    @PostMapping(value = "queryCompareBizData")
    @ApiOperation(value = "查询核对数据列表")
    public void queryCompareBizData(CompareBizParam compareBizParam, PageParam pageParam) {
        logger.info("查询核对数据列表,参数：{}",compareBizParam);

        Page page = bizCheckService.queryCompareBizData(compareBizParam, pageParam, getCurUserAccount());
        this.setPageBean(page);
        logger.info("查询核对数据列表,结果：{}", page);
    }


    @PostMapping(value = "exportCompareData")
    @ApiOperation(value = "导出核对数据列表")
    public void exportCompareData(CompareBizParam compareBizParam,HttpServletResponse response) throws IOException {
        ExcelInfo excelInfo = bizCheckService.exportCompareData(compareBizParam,getCurUserAccount());

        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response,excelInfo,getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }
}
